package com.gkt.ec.cms.activity.service.impl;

import com.github.pagehelper.PageInfo;
import com.gkt.ec.cms.activity.dao.MarketActivityDao;
import com.gkt.ec.cms.activity.model.domain.CompanyActivityEntity;
import com.gkt.ec.cms.activity.model.domain.CompanyEntity;
import com.gkt.ec.cms.activity.model.domain.CustomerActivityEntity;
import com.gkt.ec.cms.activity.model.domain.MarketActivityEntity;
import com.gkt.ec.cms.activity.model.pojo.SearchActivityVO;
import com.gkt.ec.cms.activity.service.ActivityService;
import com.gkt.ec.commons.shared.commons.service.CrudService;

import org.apache.ibatis.session.RowBounds;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * Created by 14081593 on 2015/11/30.
 */
@Service("ActivityService")
public class ActivityServiceImpl extends CrudService<MarketActivityDao, MarketActivityEntity> implements ActivityService {

    @Transactional(readOnly = false)
    public void saveActivity(MarketActivityEntity entity) {
        if (entity.getId() == null) {
            dao.addActivity(entity);
        } else {
            dao.updateActivity(entity);
        }
    }

    @Transactional(readOnly = false)
    public int addCompanyActivity(CompanyActivityEntity ca) {
        return dao.addCompanyActivity(ca);
    }

    @Transactional(readOnly = false)
    public int addCustomerActivity(CustomerActivityEntity ca) {
        return dao.addCustomerActivity(ca);
    }

    public PageInfo<MarketActivityEntity> queryActivityList(SearchActivityVO searchCondition, int offset, int max) {
        List<MarketActivityEntity> entityList = dao.queryActivityList(searchCondition, new RowBounds(offset, max));
        return new PageInfo<MarketActivityEntity>(entityList);
    }

    /**
     * 更新给参与活动的客户发送短信的状态信息
     *
     * @param customerActivityId 客户活动记录ID
     * @param smsSendStatus      发送状态 0：未发送 1：发送成功 2：发送失败
     * @param sentTime           发送时间
     */
    @Transactional
    public void updateSendSmsCustomerStatus(Long customerActivityId, Integer smsSendStatus, Date sentTime) {
        dao.updateSendSmsCustomerStatus(customerActivityId, smsSendStatus, sentTime);
    }

    /**
     * 更新活动发送短信任务完成情况信息
     *
     * @param activityId    活动ID
     * @param smsSendStatus 发送状态 0：未发送 1：发送成功 2：发送失败
     * @param beginTime     开始时间
     * @param endTime       结束时间
     */
    @Transactional
    public void updateSendSmsActivityStatus(Long activityId, Integer smsSendStatus, Date beginTime, Date endTime) {
        dao.updateSendSmsActivityStatus(activityId, smsSendStatus, beginTime, endTime);
    }

    /**
     * 查询客户活动记录
     *
     * @param activityId 活动ID
     * @return 客户活动记录列表
     */
    public List<CustomerActivityEntity> queryAllCustomerActivity(Long activityId) {
        return dao.queryAllCustomerActivity(activityId);
    }

    /**
     * 查询需要发送短信的任务列表
     *
     * @return 活动列表
     */
    public List<MarketActivityEntity> querySendSmsActivity() {
        return dao.querySendSmsActivity();
    }

    /**
     * 查询活动详情
     */
    public MarketActivityEntity getActivityDetail(Long id) {
        return dao.getActivityDetail(id);
    }

    /**
     * 删除活动
     */
    @Transactional
    public int delActivity(HashMap paramMap) {
        return dao.delActivity(paramMap);
    }


    public List<CompanyEntity> getCompanyByActivityId(Long activityId) {
        return dao.getCompanyByActivityId(activityId);
    }

    @Transactional
    public int cancelCustomerActivity(CustomerActivityEntity ca) {
        return dao.cancelCustomerActivity(ca);
    }

    @Transactional
    public int cancelCompanyActivity(CompanyActivityEntity ca) {
        return dao.cancelCompanyActivity(ca);
    }
}
